package com.kitty.record.kittyalgorithm.ds.stack;

import java.lang.reflect.Array;

/**
 * @author shenshen.qian@amh-group.com
 * @version 0.0.1
 * @date 2022/2/9 19:37
 */
public class MyArrayStack<T> {

    private static final int DEFAULT_SIZE = 12;

    private T[] st;

    private int sCount;

    public MyArrayStack(Class<T> type, int size) {
        st = (T[]) Array.newInstance(type, size);
        sCount = 0;
    }

    public MyArrayStack(Class<T> type) {
        this(type, DEFAULT_SIZE);
    }


    /**
     * 将元素添加到栈中
     *
     * @param val
     */
    public void push(T val) {
        st[sCount++] = val;
    }

    /**
     * 返回栈顶元素值，并删除元素
     *
     * @return
     */
    public T pop() {
        T t = st[sCount - 1];
        sCount--;
        return t;
    }

    /**
     * 返回栈顶元素值
     *
     * @return
     */
    public T peek() {
        if (sCount == 0) {
            throw new StackOverflowError("栈溢出啦");
        }
        return st[sCount - 1];
    }

    /**
     * 返回栈的大小
     *
     * @return
     */
    public int size() {
        return sCount;
    }

    /**
     * 栈是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        return sCount == 0;
    }


}
